package com.edunera.connection;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class Connection {
	//local
	private static String base="http://swe.cmpe.boun.edu.tr:8180";	
	
	//private static String base="http://192.168.2.13:9000";
	// live private static String baseUrl="http://swe.cmpe.boun.edu.tr:8180";

// live 
	//private String baseUrl="http://swe.cmpe.boun.edu.tr:8180";
	public static boolean isUserConnectedBefore(String user_device_id){
			
			String url=base+"/LoginWithAndroidDevice/"+user_device_id;

			JSONObject result= getJSONfromURL(url);
			try{
				String res=result.getString("status");
				if(res.equals("failed"))
					return false;
				else if(res.equals("successfull"))
					return true;
			}catch(JSONException e){
				Log.d("e:1",e.getMessage());
				return false;
			}
			return false;
	}
	 
	public static boolean loginToEdunera(String username, String password,String user_device_id){
		 String url=base+"/mobileLogin?email="+username+"&password="+password+"&user_device_id="+user_device_id;
			
			JSONObject result= getJSONfromURL(url);
			try{
				String res=result.getString("status");
				if(res.equals("failed"))
					return false;
				else if(res.equals("successfull"))
					return true;
			}catch(JSONException e){
				Log.d("e:1",e.getMessage());
				return false;
			}
			return false;
	 }
	
	public static JSONArray getUserCourses(String user_device_id){
		String url=base+"/mobileCourseList/"+user_device_id;
		
		return getJSONArrfromURL(url);
		
		
	}
	
	public static JSONObject getCourseById(String Id,String userDeviceId){
		
		String url=base+"/getCourseDetails?Id=%s&userDeviceId=%s";
		
		url=String.format(url, Id,userDeviceId);
		
		return getJSONfromURL(url);
	}

	private static JSONObject getJSONfromURL(String url){

			//initialize
			InputStream is = null;
			String result = "";
			JSONObject jArray = null;

			//http post
			try{
				HttpClient httpclient = new DefaultHttpClient();
				HttpGet httppost = new HttpGet(url);
				HttpResponse response = httpclient.execute(httppost);
				HttpEntity entity = response.getEntity();
				is = entity.getContent();

			}catch(Exception e){
				Log.e("log_tag", "Error in http Connection "+e.toString());
			}

			//convert response to string
			try{
				BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
				StringBuilder sb = new StringBuilder();
				String line = null;
				while ((line = reader.readLine()) != null) {
					sb.append(line + "\n");
				}
				is.close();
				result=sb.toString();
			}catch(Exception e){
				Log.e("log_tag", "Error converting result "+e.toString());
			}

			//try parse the string to a JSON object
			try{
		        	jArray = new JSONObject(result);
			}catch(JSONException e){
				Log.e("log_tag", "Error parsing data "+e.toString());
			}

			return jArray;
		}
	
	private static JSONArray getJSONArrfromURL(String url){

		//initialize
		InputStream is = null;
		String result = "";
		JSONArray jArray = null;

		//http post
		try{
			HttpClient httpclient = new DefaultHttpClient();
			HttpGet httppost = new HttpGet(url);
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();

		}catch(Exception e){
			Log.e("log_tag", "Error in http Connection "+e.toString());
		}

		//convert response to string
		try{
			BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
			is.close();
			result=sb.toString();
		}catch(Exception e){
			Log.e("log_tag", "Error converting result "+e.toString());
		}

		//try parse the string to a JSON object
		try{
	        	jArray = new JSONArray(result);
		}catch(JSONException e){
			Log.e("log_tag", "Error parsing data "+e.toString());
		}

		return jArray;
	}
}
